<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * Description of Exposure_orderModel
 *
 * @author wml
 */

namespace Home\Model;

use Think\Model;

class ExposureOrderModel extends Model {
    /* 自动验证规则 */

    protected $_validate = array(
        array('package_id', "0", '请选择曝光套餐', self::MUST_VALIDATE, 'notequal'),
        array('exposure_content', "require", '请输入文案', self::MUST_VALIDATE, 'regex'),
        array('exposure_content', '0,200', '文案不能超过200个字符', self::MUST_VALIDATE, 'length'),
    );
    
    public function editData($data) {
        if (empty($data)) {
            return false;
        }
         if (!$this->create($data)) {
                $this->error = $this->getError();
                return false;
            }
        /* 添加或新增行为 */
        if (empty($data['id'])) { //新增数据
             $package =  getDataById("id=".$data["package_id"],"price" ); //获取套餐的价格
            $data["user_id"] = session("user.id");
            $data["price"] =$package["price"];
             $data["add_time"] = date("Y-m-d H:i:s");
            $imgs = uploadPhotos($_FILES, "baoguang");

            $data["exposure_imgs"] = $imgs['code'] == 100 ? "" : $imgs['mess'];
           
            $id = $this->add(); //添加行为
            if (!$id) {
                $this->error = "添加失败，请重试";
                return false;
            }
        } else { //更新数据
            $status = $this->save(); //更新基础内容
            if (false === $status) {
                $this->error = "修改失败，请重试";
                return false;
            }
        }
        return $data;
    }

    public function deleteData($map) {
        $result = $this->where($map)->delete();
        return $result;
    }

    /*
     * 根据ID查询详细信息
     *  @author  csb
     */
    public function getDataById($map, $field = "*") {
        $relust = $this->field($field)->where($map)->find();
        return $relust;
    }
    
    /*
     * 获取云曝光的列表
     * @author  csb
     */
    public function getExposureOrder($map = "1=1", $field = "*") {
        $datalist = getPages($this, $map, null, C('PAGESIZE'), null, $field);
        return $datalist;
    }
    
    /*
     * 后台获取云曝光订单的列表
     * @author  csb
     */
    public function exposureOrderlist($map="1=1", $field = "*",$join="",$alias) {
        $datalist = getPages($this, $map, null, C('PAGESIZE'), $join, $field,"e.id","",$alias);
        return $datalist;
    }
}

?>
